package org.brisling.ethic.queryService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.exception.DangerousOperatorException;
import org.brisling.common.util.StringUtil;
import org.brisling.common.util.WhereUtil;
import org.brisling.ethic.domain.GiftDomain;
import org.brisling.ethic.domain.VOGiftReturn;
import org.brisling.ethic.util.EthicUtil;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/gift/query")
public class QueryGiftService extends AbstractBaseQueryService<GiftDomain>{
	
	
	
	
	@Autowired
	private	ShiroSecurityService shiroSecurityService;
	
	@Autowired
	private	QueryDictionaryService dicService;
	
	public QueryGiftService(){
		this.moduleName = ConstSysParam.MODULE_ETHIC_GIFT;
		this.subSystem = ConstSysParam.SUBSYSTEM_ETHIC;
		this.tableName = ConstSysParam.VIEW_PREFIX +this.getTableName(GiftDomain.class);
		this.clazz = GiftDomain.class;
		typeMap = this.getTypeMap(clazz);
		
		orderMap = new LinkedHashMap<String,String>();
		
		orderMap.put("integritydate","desc");			
		orderMap.put("id",null);
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return new ModelAndView("/ethic/gift/main");
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return new ModelAndView("/ethic/gift/editDlg");
	}
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<GiftDomain> getListDomain(@RequestBody(required=false) String body){		
		
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Map<String,Object> conditionMap = this.getBodyParam(body);
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, "query_ethic_all"))
			conditionMap = this.addJobDeptConditionToMap(userid,conditionMap, "org_id");
		
		return super.getListDomainByConditionAndOrder(null, conditionMap, typeMap, orderMap, true);
		
	}
	
	@RequestMapping("/getList.action")
	public List<GiftDomain> getList(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Integer deptid,
			@RequestParam(value="sort",required=false) String sort){		
		
		StringBuilder sb = new StringBuilder();
		String sql = "";
		if(deptid!=null){
			String whereCorse=String.format(sb.append("\"deptid\" = %1$d").toString(),deptid);
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
		}else{
			sql = String.format(this.T_SQL, this.tableName);
		}
		
		return super.getPagedList(page,rows,sql);
	}
	
	public List<GiftDomain> getListAll(Map<String, String> map){	
		
		String sql = "";
		String whereCorse = null;					
			
		whereCorse = WhereUtil.getConditionWhereCorse(map,typeMap);
		System.out.println("whereCorse: " +whereCorse);		
		
		if(whereCorse!=null){			
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
			return super.getAllList(sql);
		}
		return new ArrayList<GiftDomain>();		
		
	}
	
	public List<GiftDomain> getOrderListAll(Map<String, String> map,LinkedHashMap<String,String> orderMap){	
		
		String sql = "";
		String whereCorse = null;					
		String orderCorse = null;
		whereCorse = WhereUtil.getConditionWhereCorse(map,typeMap);
		
		orderCorse = WhereUtil.getOrderCorse(orderMap);
		System.out.println("whereCorse: " +whereCorse);		
		
		if(whereCorse!=null){			
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
			
			if(orderCorse!=null){
				sql += " order by " + orderCorse;
			}
			return super.getAllList(sql);
		}		
		
		return new ArrayList<GiftDomain>();		
		
	}
	/**
	 * 根据用户id查找用户信息
	 * @param id	用户id
	 * @return 用户信息记录 
	 */
	@RequestMapping("/getOne.action")
	public GiftDomain getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据用户唯一编码查找用户信息
	 * @param identify	用户唯一编码
	 * @return 用户信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public GiftDomain getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}	
	
	
	@RequestMapping("/getListByField.action")
	public List<GiftDomain> getListByField(@RequestParam(value="field",required=false) String field,@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getListByIdentifyByGet(field,fieldVal);
	}
	
	
	@RequestMapping("/getUserGiftList.action")
	public List<GiftDomain> getUserDailyList(
			@RequestParam(value="eval_year",required=false) String eval_year,
			@RequestParam(value="uid",required=false) String uid){
		
		if(eval_year!=null && uid!=null){
			Map<String,String> pMap = new HashMap<String,String>();
			
			String[] _date = EthicUtil.getEvaluateDate(eval_year);
			pMap.put("integritydate_start", _date[0]);
			pMap.put("integritydate_end", _date[1]);
			pMap.put("person_code", uid);
			
			LinkedHashMap<String,String> oMap = new LinkedHashMap<String,String>();
			oMap.put("integritydate", "desc");
			return this.getOrderListAll(pMap,oMap);
			
		}
		return null;
	}
	
	/**
	 * 根据评价年限获取VODailyEvaluate
	 * @param eval_year
	 * @return
	 * @throws DangerousOperatorException
	 */
	
	public List<VOGiftReturn> getVOGiftByEvalateYearAndUser(
			@RequestParam(value="eval_year",required=false) String eval_year,
			@RequestParam(value="uid",required=false) String uid) throws DangerousOperatorException {
		
		if(eval_year!=null && uid!=null){
			String sql = dicService.getSqlByBizcode("gift_return");
			String[] _date = EthicUtil.getEvaluateDate(eval_year);
			
			Map<String , Object> map = new HashMap<String,Object>();
			map.put("start_date", _date[0]);
			map.put("end_date", _date[1]);
			map.put("eval_year", eval_year);
			map.put("eval_id", uid);
			sql = StringUtil.formatSql(sql, map);
			
			return super.getClassedAllList(sql, VOGiftReturn.class);	
		}
		return null;
	}
	
}
